##THIS CODE REPLICATES FIGURE 1 


################################################
##########      FIGURE 1            ############
################################################
rm(list=ls())

setwd("C:/Users/torewig/Dropbox/Exconstpaper/ACLP/Analyser")

library(foreign)
data <- read.dta("Knutsen_Wig_CPS_final_forR.dta")

#DD democracies
mean <-mean(data$lead5growthgdppc_Mad[data$democracy==1], na.rm=T)
sd  <- sd(data$lead5growthgdppc_Mad[data$democracy==1], na.rm=T)
mean <-mean(data$lead5growthgdppc_Mad[data$democracy==1], na.rm=T)
dems<- as.data.frame(cbind("Democracy as coded by DD (N=4003)", mean, sd))

#DD autocracies
mean  <- mean(data$lead5growthgdppc_Mad[data$democracy==0 ], na.rm=T)
sd  <- sd(data$lead5growthgdppc_Mad[data$democracy==0], na.rm=T)
auts <- as.data.frame(cbind("Autocracy as coded by DD (N=5134)", mean, sd))

#Pure autocracies
mean <-mean(data$lead5growthgdppc_Mad[data$democracy==0 & data$type2==0], na.rm=T)
sd  <- sd(data$lead5growthgdppc_Mad[data$democracy==0 & data$type2==0], na.rm=T)
auts2 <- as.data.frame(cbind("Indisputable autocracy (no Type II regimes) (N=3990)", mean, sd))

#type 2 regimes
mean  <- mean(data$lead5growthgdppc_Mad[data$type2==1 ], na.rm=T)
sd  <- sd(data$lead5growthgdppc_Mad[data$type2==1], na.rm=T)
typ2 <- as.data.frame(cbind("Type II regimes (N=1144)", mean, sd))

#democracy + type 2 regimes
mean  <- mean(data$lead5growthgdppc_Mad[data$type2==1 | data$democracy==1 ], na.rm=T)
sd  <- sd(data$lead5growthgdppc_Mad[data$type2==1 | data$democracy==1 ], na.rm=T)
demtyp2 <- as.data.frame(cbind("Democracy + Type II regimes (N=5147)", mean, sd))


#making numeric
dat2 <- as.data.frame(rbind(dems,auts, auts2, typ2,demtyp2))
numerize <- function(x){as.numeric(as.character(x))}
dat2[-1] <- sapply(dat2[-1], FUN=numerize)


### Plot - Labels inside graph


barplot(dat2$mean, 
        main="Average % GDP per capita growth", names.arg=varnames, las=3, horiz=T)
text(cex=1, x=dat2$mean, y=-1.25, varnames, xpd=TRUE, srt=180)

par(mar = c(7, 4, 2, 2) + 0.2) #add room for the rotated labels

dat2 = dat2[with(dat2, order(-mean)), ] #order

end_point = 0.5 + nrow(dat2) + nrow(dat2)-1 #this is the line which does the trick (together with barplot "space = 1" parameter)


pdf("regimegrowth.pdf")
b <- barplot(dat2$mean, col="gray88", 
        main="",
        ylab="Regime Classifications",
        xlab = "Average GDP per capita growth (%)", xlim=c(0,3),
        space=0.2, horiz=T)
#rotate 60 degrees, srt=60
text(0.8, b, labels=dat2$V1, cex=0.9)
dev.off()



